package com.ruoyi.web.controller.web.habit;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.WrapperResponse;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.habit.domain.dto.HabitDTO;
import com.ruoyi.system.habit.domain.entity.Habit;
import com.ruoyi.system.habit.service.HabitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: xujiabing
 * @date: 2024-10-15 9:47
 * @description: 打卡习惯控制器
 */
@Api("习惯查询管理")
@RestController
public class HabitController {

    @Autowired
    private HabitService habitService;

    /**
     * @description: 查询习惯列表
     * @author: xujiabing
     * @date: 2024-10-18 16:07
     * @param: habitDTO
     * @return: WrapperResponse<?>
     */
    @ApiOperation("查询习惯列表")
    @GetMapping("/web/habit/list")
    @PreAuthorize("@ss.hasPermi('habit:habit:list')")
    public WrapperResponse<?> list(HabitDTO habitDTO) {
        return WrapperResponse.success(habitService.list(habitDTO));
    }

    /**
     * @description: 新增习惯
     * @author: xujiabing
     * @date: 2024-10-19 22:45
     * @param: habitDTO
     * @return: WrapperResponse<?>
     */
    @ApiOperation("新增习惯")
    @PostMapping("/web/habit/add")
    @PreAuthorize("@ss.hasPermi('habit:habit:add')")
    public WrapperResponse<?> add(@RequestBody @Validated HabitDTO habitDTO) {
        return WrapperResponse.success();
    }

    /**
     * @description: 编辑习惯
     * @author: xujiabing
     * @date: 2024-10-20 14:47
     * @param: habitDTO
     * @return: WrapperResponse<?>
     */
    @ApiOperation("编辑习惯")
    @PutMapping("/web/habit/edit")
    @PreAuthorize("@ss.hasPermi('habit:habit:edit')")
    public WrapperResponse<?> update(@RequestBody @Validated HabitDTO habitDTO) {
        return WrapperResponse.success();
    }

    /**
     * @description: 删除习惯
     * @author: xujiabing
     * @date: 2024-10-20 17:06
     * @param: ids
     * @return: WrapperResponse<?>
     */
    @ApiOperation("删除习惯")
    @DeleteMapping("/web/habit/remove/{ids}")
    @PreAuthorize("@ss.hasPermi('habit:habit:remove')")
    public WrapperResponse<?> removeByIds(@PathVariable("ids") List<Integer> ids) {
        return WrapperResponse.success();
    }

    /**
     * @description: 查询习惯,根据id
     * @author: xujiabing
     * @date: 2024-10-20 17:06
     * @param: id
     * @return: WrapperResponse<Habit>
     */
    @ApiOperation("查询习惯,根据id")
    @GetMapping("/web/habit/getOne/{id}")
    @PreAuthorize("@ss.hasPermi('habit:habit:getOne')")
    public WrapperResponse<Habit> queryHabitById(@PathVariable("id") Integer id) {
        return WrapperResponse.success(habitService.queryById(id));
    }

    /**
     * @description: 导出
     * @author: xujiabing
     * @date: 2024-10-20 17:11
     * @param: response
     * @param: habitDTO
     * @return: void
     */
    @ApiOperation(("习惯导出"))
    @PostMapping("/web/habit/export")
    @PreAuthorize("@ss.hasPermi('habit:habit:export')")
    public void export(HttpServletResponse response, HabitDTO habitDTO) {
        List<Habit> list = habitService.queryByExport(habitDTO);
        ExcelUtil<Habit> util = new ExcelUtil<>(Habit.class);
        util.exportExcel(response, list, "习惯列表");
    }

    /**
     * @description: 习惯导入
     * @author: xujiabing
     * @date: 2024-10-30 20:03
     * @param: file
     * @param: updateFlag
     * @return: WrapperResponse<?>
     */
    @ApiOperation(("习惯导入"))
    @PostMapping("/web/habit/import")
    @PreAuthorize("@ss.hasPermi('habit:habit:import')")
    public WrapperResponse<?> imports(MultipartFile file, Boolean updateFlag) {
        return WrapperResponse.success(habitService.imports(file,updateFlag));
    }

    /**
     * @description: excel模板下载
     * @author: xujiabing
     * @date: 2024-10-30 17:45
     * @param: response
     * @return:
     */
    @ApiOperation(("模板导出"))
    @PreAuthorize("@ss.hasPermi('habit:habit:exportTemplate')")
    @PostMapping("/web/habit/exportTemplate")
    public void importTemplate(HttpServletResponse response){
        ExcelUtil<Habit> util = new ExcelUtil<>(Habit.class);
        util.importTemplateExcel(response, "导入模板");
    }

}
